<?php


class Model_user extends CI_Model
{
	function __construct()
	{
		parent::__construct();
	}
	
	function save()
	{
		$userinfo = $this->input->post();
		if($userinfo['password']!= $userinfo['confirm']){
			die('password confirm error!');
		}
		unset($userinfo['confirm']);  //这个字段不存在，只是为了效验两次密码是不是一样的
		
		$userinfo["password"] = md5($userinfo["password"]);
		$this->db->insert('ciuser',$userinfo);
	}
	
	function update()
	{
		$userinfo = $this->input->post();
		if(!empty($userinfo['uid'])){
			$uid = $userinfo['uid'];
			unset($userinfo['confirm']);
			unset($userinfo['uid']);
			$userinfo['password'] = md5($userinfo['password']);
			
			$this->db->where('uid',$uid)->update('ciuser',$userinfo);
		}
	}
	
	function delete($uid)
	{
		$this->db->where('uid',$uid)->delete('ciuser');
	}
	
	function checklogin()
	{
		$user = $this->input->post('username');
		$pass = $this->input->post('password');
		$this->db->select('*')->where('username',$user);
		
		if($this->db->count_all_results('ciuser')){
			$userinfo = $this->db->select('*')->where('username', $user)->get('ciuser')->result_array();
			
			if($user == $userinfo[0]['username']){
				if(md5($pass) == $userinfo[0]['password']){
					return true;
				}else{
					return false;
				}
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
}